package br.com.ifba.sadd.admin.presentation.turma {
	import br.com.ifba.sadd.model.Turma;
	import br.com.ifba.sadd.model.Curso;
	import br.com.ifba.sadd.model.Disciplina;
	import br.com.ifba.sadd.model.ModelLocator;
	import br.com.ifba.sadd.model.Professor;
	import br.com.ifba.sadd.model.ProfessorDisciplina;
	import br.com.ifba.sadd.model.ServiceLocator;

	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.ValidationResultEvent;
	import mx.rpc.AsyncToken;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	import flash.events.Event;

	[Event(name="reset", type="flash.events.Event")]
	
	[Bindable]
	public class InserirTurmaPresentationModel
	{
		
		
		private var _enable:Boolean = false;
		private var _cursos:ArrayCollection = new ArrayCollection();
		private var _professores:ArrayCollection = new ArrayCollection();
		private var _service:ServiceLocator = ServiceLocator.getInstance();
		private var _gradeDisciplinar:ArrayCollection = new ArrayCollection();

		
		public function InserirTurmaPresentationModel()
		{
		}
		
		/* ----------------- Geters e Seters ------------------*/

		public function get enable():Boolean
		{
			return _enable;
		}
		
		public function set enable(value:Boolean):void
		{
			_enable = value;
		}

		[ArrayElementType("br.com.ifba.sadd.model.ProfessorDisciplina")]
		public function get gradeDisciplinar():ArrayCollection
		{
			return _gradeDisciplinar;
		}
		
		public function set gradeDisciplinar(value:ArrayCollection):void
		{
			_gradeDisciplinar = value;
		}

		[ArrayElementType("br.com.ifba.sadd.model.Curso")]
		public function get cursos():ArrayCollection
		{
			return _cursos;
		}
		
		public function set cursos(value:ArrayCollection):void
		{
			_cursos = value;
		}
		
		[ArrayElementType("br.com.ifba.sadd.model.Professor")]
		public function get professores():ArrayCollection
		{
			return _professores;
		}
		
		public function set professores(value:ArrayCollection):void
		{
			_professores = value;
		}
		
		/* --------------------- Funções de Crud ----------------------*/
		
			
		public function inserir(nome:String, curso:Curso):void
		{
			var turma:Turma = new Turma();
			turma.curso = curso;
			turma.nome = nome.toUpperCase();
			turma.gradeDisciplinar = gradeDisciplinar;
			var token:AsyncToken = _service.turmaService.inserirTurma(turma);
			token.addResponder(new Responder(handleInserir, handleFailure));
		}		
		

		/* ---------------------- Funções de Apoio ----------------------*/
		
			
		public function initInserir():void
		{
			listarCursos();
			listarProfessores();
		}
		
		private function listarCursos():void
		{
			var token:AsyncToken = _service.cursoService.listarCursos();
			token.addResponder(new Responder(handleCursos, handleFailure));
		}
			
		private function listarProfessores():void
		{
			var token:AsyncToken = _service.professorService.listarProfessores();
			token.addResponder(new Responder(handleProfessores, handleFailure));
		}	
				
		public function validarCadastro(string:ValidationResultEvent, number:ValidationResultEvent):void
		{
			if((string.type == ValidationResultEvent.VALID) && (number.type == ValidationResultEvent.VALID))
			{
				enable = true;
			}
			else{
				enable = false;
			}	
		}
		
		public function adicionarNaGrade(disciplina:Disciplina, professor:Professor):void
		{
			if(disciplina != null && professor != null){
				var existe:Boolean = false;
				for each (var pd:Disciplina in gradeDisciplinar)
				{
					if(pd.nome == disciplina.nome)
					{
						Alert.show("Disciplina já adicionada");
						existe = true;
						break;
					}
					else
					{
						existe = false;
					}
				}
				if(!existe){
					var professorDisciplina:ProfessorDisciplina = new ProfessorDisciplina();
					professorDisciplina.professor = professor;
					professorDisciplina.disciplina = disciplina;
				}
			}
			else{
				Alert.show("Selecione uma disciplina e um professor");
			}
		}
		
		public function removerDaGrade(index:int):void
		{
			if(index < 0)
			{
				Alert.show("Selecione uma disciplina");
			}
			else{				
				gradeDisciplinar.removeItemAt(index);
			}
		}
		
		/* ------------------------ handles ------------------------*/		
		
		private function handleCursos(list:ResultEvent):void
		{
			cursos = new ArrayCollection(list.result as Array);
		}
			
		private function handleProfessores(list:ResultEvent):void
		{
			professores = new ArrayCollection(list.result as Array);
		}
		
		private function handleFailure(event:FaultEvent):void
		{
			Alert.show(event.fault.faultString, "Erro!");
		}
		
		private function handleInserir(event:ResultEvent):void
		{			
			Alert.show("Cadastro inserido com sucesso!");
			dispatchEvent(new Event("reset"));
		}
	}
}